package org.dromara.utils.service;

import com.alibaba.excel.metadata.csv.CsvCellStyle;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.*;
import org.dromara.pe.domain.SportScore;

import java.util.List;

public class CustomCellWriteHandler implements CellWriteHandler {
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
    }


//
   @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<com.alibaba.excel.metadata.data.WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
//        // 如果是表头行
//        if (relativeRowIndex == 0) {
//            // 设置表头背景色为灰色
//            WriteCellStyle writeCellStyle = new WriteCellStyle();
//            CellStyle cellStyle = new CsvCellStyle(writeCellStyle);
//            cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//            cell.setCellStyle(cellStyle);
//        }

        // 如果不是表头行（即数据行），处理体测成绩写入逻辑
        if (relativeRowIndex > 0) {
            // 获取当前行对应的Student对象（这里假设数据按顺序依次写入，可根据实际调整）
            List<WriteHandler> writeHandlerList = writeSheetHolder.getWriteHandlerList();
//            List<Student> dataList = writeSheetHolder.getWriteHandlerList();
//            Student currentStudent = dataList.get(relativeRowIndex - 1);
//            int baseColumnIndex = 6; // 假设基本信息占前6列，体测成绩从第7列开始
//            List<SportScore> sportScoreList = currentStudent.getSportScores();
//            for (int i = 0; i < sportScoreList.size(); i++) {
//                SportScore sportScore = sportScoreList.get(i);
//                if (cell.getColumnIndex() >= baseColumnIndex + i * 4 && cell.getColumnIndex() < baseColumnIndex + (i + 1) * 4) {
//                    if (cell.getColumnIndex() - baseColumnIndex - i * 4 == 0) {
//                        cell.setCellValue(sportScore.getSportRaw());
//                    } else if (cell.getColumnIndex() - baseColumnIndex - i * 4 == 1) {
//                        cell.setCellValue(sportScore.getSportScore());
//                    } else if (cell.getColumnIndex() - baseColumnIndex - i * 4 == 2) {
//                        // 这里假设没有记录(cm)数据，可根据实际修改
//                        cell.setCellValue("");
//                    } else {
//                        // 这里假设没有记录(次)数据，可根据实际修改
//                        cell.setCellValue("");
//                    }
//                }
//            }
        }
    }
}
